package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaCompany;
import com.sailfish.springbootdemo.pojo.db1.PcbaWorkshop;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaWorkshopDao {

    /**
     * 查询所有的
     *
     * @param searchValue
     * @return
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_WORKSHOP + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and pcba_workshop_name_cn like CONCAT('%',#{searchValue},'%') or pcba_workshop_name_en like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            " order by sort" +
            "</script>")
    @Results(id = "pcbaWorkshopResultMap", value = {
            @Result(column = "pcba_workshop_id", property = "pcbaWorkshopId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_company_id", property = "pcbaCompanyId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_workshop_name_cn", property = "pcbaWorkshopNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_workshop_name_en", property = "pcbaWorkshopNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    List<PcbaWorkshop> getAllPcbaWorkshop(@Param("searchValue") String searchValue);

    /**
     * 根据公司id查询
     *
     * @param pcbaCompanyId
     * @return
     */
    @ResultMap(value = "pcbaWorkshopResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_WORKSHOP + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and pcba_workshop_name_cn like CONCAT('%',#{searchValue},'%') or pcba_workshop_name_en like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            "<if test='pcbaCompanyId != null and pcbaCompanyId != \"\" '>" +
            " and pcba_company_id = #{pcbaCompanyId}" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<PcbaWorkshop> getAllPcbaWorkshopByCompanyId(@Param("searchValue") String searchValue, @Param("pcbaCompanyId") Integer pcbaCompanyId);

    @ResultMap(value = "pcbaWorkshopResultMap")
    @Select("select * from " + DB1TableName.PCBA_WORKSHOP + " where pcba_workshop_name_cn = #{pcbaWorkshopNameCn} and pcba_company_id = #{pcbaCompanyId}")
    List<PcbaWorkshop> getWorkshopByCompanyIdAndName(@Param("pcbaWorkshopNameCn") String pcbaWorkshopNameCn, @Param("pcbaCompanyId") Integer pcbaCompanyId);

    /**
     * 判断有没有重复的
     *
     * @param pcbaCompanyId
     * @return
     */
    @ResultMap(value = "pcbaWorkshopResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_WORKSHOP + " where pcba_workshop_name_cn = #{pcbaWorkshopNameCn} and pcba_company_id = #{pcbaCompanyId} " +
            "<if test='pcbaWorkshopId != null and pcbaWorkshopId != \"\" '>" +
            " and pcba_workshop_id != #{pcbaWorkshopId}" +
            "</if>" +
            "</script>")
    List<PcbaCompany> judgeUniquePcbaWorkshop(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("pcbaWorkshopId") Integer pcbaWorkshopId, @Param("pcbaWorkshopNameCn") String pcbaWorkshopNameCn);


    /**
     * 删除
     *
     * @param pcbaWorkshopId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_WORKSHOP + " where pcba_workshop_id = #{pcbaWorkshopId}")
    Integer deleteWorkshop(@Param("pcbaWorkshopId") Integer pcbaWorkshopId);

    /**
     * 删除
     *
     * @param pcbaCompanyId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_WORKSHOP + " where pcba_company_id = #{pcbaCompanyId}")
    Integer deleteWorkshopByCompanyId(@Param("pcbaCompanyId") Integer pcbaCompanyId);

    @ResultMap(value = "pcbaWorkshopResultMap")
    @Select("select * from " + DB1TableName.PCBA_WORKSHOP + " where pcba_workshop_id = #{pcbaWorkshopId}")
    PcbaWorkshop getPcbaWorkshopById(@Param("pcbaWorkshopId") Integer pcbaWorkshopId);

    /**
     * 新增
     *
     * @param pcbaWorkshop
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_WORKSHOP + "(pcba_company_id,pcba_workshop_name_cn,pcba_workshop_name_en,status,sort,create_time,notes) values " +
            "(#{pcbaWorkshop.pcbaCompanyId},#{pcbaWorkshop.pcbaWorkshopNameCn},#{pcbaWorkshop.pcbaWorkshopNameEn},#{pcbaWorkshop.status},#{pcbaWorkshop.sort},#{pcbaWorkshop.createTime},#{pcbaWorkshop.notes})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbaWorkshop.pcbaWorkshopId")
    Integer addPcbaWorkshop(@Param("pcbaWorkshop") PcbaWorkshop pcbaWorkshop);

    /**
     * 更新
     *
     * @param pcbaWorkshop
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_WORKSHOP +
            "<set>" +
            "<if test='pcbaWorkshop.pcbaCompanyId != null and pcbaWorkshop.pcbaCompanyId != \"\" '>" +
            " pcba_company_id = #{pcbaWorkshop.pcbaCompanyId}," +
            "</if>" +
            "<if test='pcbaWorkshop.pcbaWorkshopNameCn != null and pcbaWorkshop.pcbaWorkshopNameCn != \"\" '>" +
            " pcba_workshop_name_cn = #{pcbaWorkshop.pcbaWorkshopNameCn}," +
            "</if>" +
            "<if test='pcbaWorkshop.pcbaWorkshopNameEn != null and pcbaWorkshop.pcbaWorkshopNameEn != \"\" '>" +
            " pcba_workshop_name_en = #{pcbaWorkshop.pcbaWorkshopNameEn}," +
            "</if>" +
            "<if test='pcbaWorkshop.status != null and pcbaWorkshop.status != \"\" '>" +
            " status = #{pcbaWorkshop.status}," +
            "</if>" +
            "<if test='pcbaWorkshop.sort != null and pcbaWorkshop.sort != \"\" '>" +
            " sort = #{pcbaWorkshop.sort}," +
            "</if>" +
            "<if test='pcbaWorkshop.notes != null and pcbaWorkshop.notes != \"\" '>" +
            " notes = #{pcbaWorkshop.notes}," +
            "</if>" +
            "<if test='pcbaWorkshop.updateTime != null '>" +
            " update_time = #{pcbaWorkshop.updateTime}," +
            "</if>" +
            "</set>" +
            " where pcba_workshop_id = #{pcbaWorkshop.pcbaWorkshopId}" +
            "</script>")
    Integer updatePcbaWorkshop(@Param("pcbaWorkshop") PcbaWorkshop pcbaWorkshop);

}
